//
//  JMActionSheet.m
//  ShareClien
//
//  Created by xiaorui on 15-1-7.
//  Copyright (c) 2015年 xiaorui. All rights reserved.
//

#import "JMSeletedCitySheet.h"
#import "JMHeader.h"


@interface JMSeletedCitySheet ()<UIPickerViewDataSource,UIPickerViewDelegate>{
    UIView *pointToView;
    NSString *backTimeString;
    NSString *backTimeString2;
    NSArray *cityArray;
    NSArray *priArray;
}

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) NSString *leftTitle;
@property (nonatomic,strong) NSString *rightTitle;
@property (nonatomic,strong) UIView *lv;

@property (nonatomic,strong) UIPickerView *pickerView;

@end

@implementation JMSeletedCitySheet
@synthesize leftTitle = _leftTitle;
@synthesize rightTitle = _rightTitle;
@synthesize bgView = _bgView;
@synthesize delegate = _delegate;


-(id)initWithLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle{
    self = [self initWithFrame:CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT)];
    if(self){
        _leftTitle = leftTitle;
        _rightTitle = rightTitle;
        NSString *path = [[NSBundle mainBundle]pathForResource:@"city" ofType:@"plist"];
        priArray = [NSArray arrayWithContentsOfFile:path];
        cityArray = priArray[0][@"cities"];
    }
     return self;
}
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        self.bgView =[[UIView alloc]initWithFrame:CGRectMake(0, -64, frame.size.width, frame.size.height)];
        self.bgView.userInteractionEnabled = YES;
        self.bgView.backgroundColor = UIColorFromRGB(0x2c2c2c);
        self.bgView.alpha = 0;
        backTimeString = @"";
        backTimeString2 = @"";
        [self addSubview:self.bgView];
       
        UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureRecogizer)];
        [self.bgView addGestureRecognizer:tap];
        
        self.lv  =[[UIView alloc]initWithFrame:CGRectMake(0, UI_SCREEN_HEIGHT - 240, UI_SCREEN_WITH, 240)];
        self.lv.backgroundColor =[UIColor whiteColor];
         self.lv.transform = CGAffineTransformMakeTranslation(0,UI_SCREEN_HEIGHT);
        [self addSubview:self.lv];
        
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WITH, 40)];
        v.backgroundColor = UIColorFromRGB(0xe9e9e9);
        [self.lv addSubview:v];
        
        UIButton *bnt = [UIButton buttonWithType:UIButtonTypeCustom];
        bnt.frame = CGRectMake(10, 3, 50, 37);
        [bnt setTitle:@"取消" forState:UIControlStateNormal];
        [bnt setTitleColor:UIColorFromRGB(0xa8a8a8) forState:UIControlStateNormal];
        [bnt addTarget:self action:@selector(dissMiss) forControlEvents:UIControlEventTouchUpInside];
        [self.lv addSubview:bnt];
        
        UIButton *bnt2 = [UIButton buttonWithType:UIButtonTypeCustom];
        bnt2.frame = CGRectMake(self.lv.frame.size.width - 60, 3, 50, 37);
        [bnt2 setTitle:@"完成" forState:UIControlStateNormal];
        [bnt2 setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
        [bnt2 addTarget:self action:@selector(compele) forControlEvents:UIControlEventTouchUpInside];
        [self.lv addSubview:bnt2];
        
        self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, self.lv.frame.size.width, self.lv.frame.size.height - 40)];
        self.pickerView.delegate = self;
        self.pickerView.showsSelectionIndicator = YES;
        self.pickerView.dataSource = self;
        [self.lv addSubview:self.pickerView];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
    }
    return self;
}
-(void)dissMiss{
    [self tapGestureRecogizer];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(component == 0){
      return   priArray.count;
    }else{
        return cityArray.count;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(component == 0){
        return priArray[row][@"state"];
    }else{
        return cityArray[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component == 0){
         cityArray = priArray[row][@"cities"];
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
        [self.pickerView reloadAllComponents];
        backTimeString = priArray[row][@"state"];
    }else{
        backTimeString2 = cityArray[row];
    }
}
- (void)compele{
    if([backTimeString isEqualToString:@""]){
        backTimeString = [NSString stringWithFormat:@"%@%@",priArray[0][@"state"],priArray[0][@"cities"][0]];
        [_delegate seletedCompletCityButton:backTimeString city:backTimeString2];
    }
    if([_delegate respondsToSelector:@selector(seletedCompletCityButton: city:)]){
        [_delegate seletedCompletCityButton:backTimeString city:backTimeString2];
    }
    [self tapGestureRecogizer];
}
-(void)showInView:(UIView *)aView{
    pointToView = aView;
    [self.pickerView reloadAllComponents];
    [aView addSubview:self];
}

-(void)willMoveToWindow:(UIWindow *)newWindow{
    [UIView transitionWithView:self.lv
                          duration:0.35
                           options:UIViewAnimationOptionCurveLinear
                        animations:
         ^{
             self.lv.transform = CGAffineTransformMakeTranslation(0,0);
             self.bgView.alpha = 0.5;
         }
                        completion:NULL];
}

-(void)tapGestureRecogizer{
    
    [UIView transitionWithView:self.lv duration:0.3 options:UIViewAnimationOptionCurveLinear animations:^{
        self.lv.transform = CGAffineTransformMakeTranslation(0,UI_SCREEN_HEIGHT - 260);
        
        
    } completion:^(BOOL finished) {
        [UIView transitionWithView:self.lv
                          duration:0.35
                           options:UIViewAnimationOptionCurveLinear
                        animations:
         ^{
             [self removeFromSuperview];
         }
                        completion:NULL];
        
    }];

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
